package org.jeecg.modules.demo.kcmx.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.kcmx.entity.JxcChuku;
import org.jeecg.modules.demo.kcmx.entity.JxcKcmx;
import org.jeecg.modules.demo.kcmx.entity.JxcRuku;
import org.jeecg.modules.demo.kcmx.entity.JxcYsk;
import org.jeecg.modules.demo.kcmx.service.IJxcChukuService;
import org.jeecg.modules.demo.kcmx.service.IJxcKcmxService;
import org.jeecg.modules.demo.kcmx.service.IJxcYskService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 应收款项
 * @Author: jeecg-boot
 * @Date:   2024-07-02
 * @Version: V1.0
 */
@Api(tags="应收款项")
@RestController
@RequestMapping("/fk/jxcYsk")
@Slf4j
public class JxcYskController extends JeecgController<JxcYsk, IJxcYskService> {
	@Autowired
	private IJxcYskService jxcYskService;
	@Autowired
	private IJxcChukuService jxcChukuService;
	 @Autowired
	 private IJxcKcmxService jxcKcmxService;

	/**
	 * 分页列表查询
	 *
	 * @param jxcYsk
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "应收款项-分页列表查询")
	@ApiOperation(value="应收款项-分页列表查询", notes="应收款项-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JxcYsk>> queryPageList(JxcYsk jxcYsk,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JxcYsk> queryWrapper = QueryGenerator.initQueryWrapper(jxcYsk, req.getParameterMap());
		Page<JxcYsk> page = new Page<JxcYsk>(pageNo, pageSize);
		IPage<JxcYsk> pageList = jxcYskService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param jxcYsk
	 * @return
	 */
	@AutoLog(value = "应收款项-添加")
	@ApiOperation(value="应收款项-添加", notes="应收款项-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JxcYsk jxcYsk) {
		Date date = new Date();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String id = "ZC"+df2.format(date);
		String zcOnly = jxcYsk.getZcOnly();
		//数量
		String zcShiji = jxcYsk.getZcShiji();
		//单价
		String zcNew = jxcYsk.getZcNew();
		String localtime = df1.format(date);
		JxcChuku jxcChuku = new JxcChuku();
		jxcChuku.setId(id);
		jxcChuku.setZcOnly(zcOnly);
		jxcChuku.setZcName(zcOnly);
		jxcChuku.setZcShiji(zcShiji);
//		jxcChuku.setZcYuji(zcShiji);
		jxcChuku.setZcOld(zcNew);
		jxcChuku.setCreatTime(localtime);
		jxcChuku.setCompanyId(jxcYsk.getCompanyId());
		jxcChukuService.save(jxcChuku);

		JxcKcmx jxcKcmx = jxcKcmxService.getByOnly(zcOnly, jxcYsk.getCompanyId());
		if (jxcKcmx == null) {
			JxcKcmx jxcKcmx1 = new JxcKcmx();
			jxcKcmx1.setId(id);
			jxcKcmx1.setZcOnly(zcOnly);
			jxcKcmx1.setZcName(zcOnly);
			jxcKcmx1.setZcLittle(zcOnly);
			jxcKcmx1.setZcRuku("0");
			jxcKcmx1.setZcChuku(zcShiji);
			jxcKcmx1.setZcKucun("-"+zcShiji);
			jxcKcmx1.setZcOld(zcNew);
			jxcKcmx1.setZcSize("1");
			jxcKcmx1.setZcNew(zcNew);
			jxcKcmxService.save(jxcKcmx1);
		}else {
			String zcKucun = jxcKcmx.getZcKucun();
			String zcChuku = jxcKcmx.getZcChuku();
//			String newKucun = String.valueOf(Double.parseDouble(zcKucun)-Double.parseDouble(zcShiji));
//			String newChuku = String.valueOf(Double.parseDouble(zcChuku)+Double.parseDouble(zcShiji));
			// 使用BigDecimal进行精确计算并设定小数位数为0
			BigDecimal kucun = new BigDecimal(zcKucun);
			BigDecimal shiji = new BigDecimal(zcShiji);
			BigDecimal newKucunBD = kucun.subtract(shiji).setScale(0, RoundingMode.HALF_UP);
			String newKucun = newKucunBD.toString();

			BigDecimal chuku = new BigDecimal(zcChuku);
			BigDecimal newChukuBD = chuku.add(shiji).setScale(0, RoundingMode.HALF_UP);
			String newChuku = newChukuBD.toString();
			System.out.println("zcKucun"+zcKucun);
			System.out.println("zcChuku"+zcChuku);
			System.out.println("newKucun"+newKucun);
			System.out.println("newChuku"+newChuku);
			jxcKcmxService.setChuku(zcOnly, newChuku,newKucun,jxcYsk.getCompanyId());
		}
		String zcOldpay = String.valueOf(Double.parseDouble(zcShiji)*Double.parseDouble(zcNew));
		jxcYsk.setId(id);
		jxcYsk.setZcOldpay(zcOldpay);
		jxcYsk.setZcTime(localtime);
		jxcYskService.save(jxcYsk);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param jxcYsk
	 * @return
	 */
	@AutoLog(value = "应收款项-编辑")
	@ApiOperation(value="应收款项-编辑", notes="应收款项-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JxcYsk jxcYsk) {
		String id = jxcYsk.getId();
		String zcOnly = jxcYsk.getZcOnly();
		//修改后的数量
		String zcShiji = jxcYsk.getZcShiji();
		//修改后的单价
		String zcNew = jxcYsk.getZcNew();
		JxcYsk oldysk = jxcYskService.getById(id);
		JxcChuku jxcChuku = jxcChukuService.getById(id);
		JxcKcmx jxcKcmx = jxcKcmxService.getByOnly(zcOnly, jxcYsk.getCompanyId());
		//原来的数量
		String zcShiji1 = oldysk.getZcShiji();
		BigDecimal zcShiji1BD = new BigDecimal(zcShiji1);
		BigDecimal zcShijiBD = new BigDecimal(zcShiji);
		//如果比原来的数量多
		// 如果比原来的数量多
		if (zcShijiBD.compareTo(zcShiji1BD) > 0) {
			// 数量差值
			BigDecimal slchazhiBD = zcShijiBD.subtract(zcShiji1BD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal currentZcShijiBD = new BigDecimal(jxcChuku.getZcShiji());
			BigDecimal updatedZcShijiBD = currentZcShijiBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			jxcChuku.setZcShiji(updatedZcShijiBD.toPlainString());
			jxcChuku.setZcOld(zcNew);
			jxcChukuService.updateById(jxcChuku);

			BigDecimal chukuBD = new BigDecimal(jxcKcmx.getZcChuku());
			BigDecimal kucunBD = new BigDecimal(jxcKcmx.getZcKucun());
			BigDecimal updatedChukuBD = chukuBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal updatedKucunBD = kucunBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);

			jxcKcmxService.setChuku(
					zcOnly,
					updatedChukuBD.toPlainString(),
					updatedKucunBD.toPlainString(),
					jxcYsk.getCompanyId()
			);
		}

// 如果比原来的数量少
		if (zcShijiBD.compareTo(zcShiji1BD) < 0) {
			// 数量差值
			BigDecimal slchazhiBD = zcShiji1BD.subtract(zcShijiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal currentZcShijiBD = new BigDecimal(jxcChuku.getZcShiji());
			BigDecimal updatedZcShijiBD = currentZcShijiBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			jxcChuku.setZcShiji(updatedZcShijiBD.toPlainString());
			jxcChuku.setZcOld(zcNew);
			jxcChukuService.updateById(jxcChuku);

			BigDecimal chukuBD = new BigDecimal(jxcKcmx.getZcChuku());
			BigDecimal kucunBD = new BigDecimal(jxcKcmx.getZcKucun());
			BigDecimal updatedChukuBD = chukuBD.subtract(slchazhiBD).setScale(0, RoundingMode.HALF_UP);
			BigDecimal updatedKucunBD = kucunBD.add(slchazhiBD).setScale(0, RoundingMode.HALF_UP);

			jxcKcmxService.setChuku(
					zcOnly,
					updatedChukuBD.toPlainString(),
					updatedKucunBD.toPlainString(),
					jxcYsk.getCompanyId());
		}
		//如果跟原来的数量相同
		if (Double.parseDouble(zcShiji)==Double.parseDouble(zcShiji1)){
			jxcChuku.setZcOld(zcNew);
			jxcChukuService.updateById(jxcChuku);
		}
		jxcYsk.setZcOldpay(String.valueOf(Double.parseDouble(zcShiji)*Double.parseDouble(zcNew)));
		jxcYskService.updateById(jxcYsk);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "应收款项-通过id删除")
	@ApiOperation(value="应收款项-通过id删除", notes="应收款项-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcYskService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "应收款项-批量删除")
	@ApiOperation(value="应收款项-批量删除", notes="应收款项-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcYskService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "应收款项-通过id查询")
	@ApiOperation(value="应收款项-通过id查询", notes="应收款项-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JxcYsk> queryById(@RequestParam(name="id",required=true) String id) {
		JxcYsk jxcYsk = jxcYskService.getById(id);
		if(jxcYsk==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jxcYsk);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcYsk
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcYsk jxcYsk) {
        return super.exportXls(request, jxcYsk, JxcYsk.class, "应收款项");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcYsk.class);
    }

}
